package com.qiezzi.eggplant.main.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.qiezzi.eggplant.R;
import com.qiezzi.eggplant.base.BaseActivity;
import com.qiezzi.eggplant.base.fragment.Appointment_fragment;
import com.qiezzi.eggplant.base.fragment.HomeFfragment;
import com.qiezzi.eggplant.base.fragment.My_fragment;
import com.qiezzi.eggplant.base.fragment.Patient_fragment;


public class MainActivity extends BaseActivity implements View.OnClickListener {
    //四个按钮
    public static final int MAIN_BUTTON = 0;
    public static final int PATIRENT_BUTTON = 1;
    public static final int APPOINTMENT_BUTTON = 2;
    public static final int MY_BUTTON = 3;

    //底部按钮的外面控件布局
    RelativeLayout rl_container_group;
    RelativeLayout rl_container_patient;
    RelativeLayout rl_container_appointment;
    RelativeLayout rl_container_my;
    //底部按钮的控件布局
    Button btn_main_group;
    Button btn_mian_patient;
    Button btn_mian_appointment;
    Button btn_main_my;
    //fragment
    HomeFfragment homeFfragment;
    Patient_fragment patient_fragment;
    Appointment_fragment appointment_fragment;
    My_fragment my_fragment;

    protected FragmentManager fragmentmanager;
    protected FragmentTransaction fragmenttransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initHeader();
        initWidget();
        setWidgetState();
        getData();
    }


    @Override
    public void initHeader() {
        inittHeaderWidget();
        setTitle("茄子口腔云");

    }

    @Override
    public void initWidget() {
        //底部按钮外层控件
        rl_container_group = (RelativeLayout) findViewById(R.id.rl_container_group);
        rl_container_patient = (RelativeLayout) findViewById(R.id.rl_container_patient);
        rl_container_appointment = (RelativeLayout) findViewById(R.id.rl_container_appointment);
        rl_container_my = (RelativeLayout) findViewById(R.id.rl_container_my);
        //底部按钮控件
        btn_main_group = (Button) findViewById(R.id.btn_main_group);
        btn_mian_patient = (Button) findViewById(R.id.btn_mian_patient);
        btn_mian_appointment = (Button) findViewById(R.id.btn_mian_appointment);
        btn_main_my = (Button) findViewById(R.id.btn_main_my);

        initFragment();
    }

    @Override
    public void setWidgetState() {

        rl_container_group.setOnClickListener(this);
        rl_container_patient.setOnClickListener(this);
        rl_container_appointment.setOnClickListener(this);
        rl_container_my.setOnClickListener(this);

        btn_main_group.setOnClickListener(this);
        btn_mian_patient.setOnClickListener(this);
        btn_mian_appointment.setOnClickListener(this);
        btn_main_my.setOnClickListener(this);
    }

    @Override
    public void getData() {


    }


    //初始化默认fragment
    public void initFragment() {

        homeFfragment = new HomeFfragment();
        fragmentmanager = getSupportFragmentManager();
        fragmenttransaction = fragmentmanager.beginTransaction();
        fragmenttransaction.add(R.id.fl_mian_frag, homeFfragment, "first");
        fragmenttransaction.commit();
        setStatue(MAIN_BUTTON);
    }


    //设置被选中的状态
    //statue 表示那个按钮被选中
    public void setStatue(int statue) {

        switch (statue) {
            case 0:
                btn_main_group.setSelected(true);
                btn_mian_patient.setSelected(false);
                btn_mian_appointment.setSelected(false);
                btn_main_my.setSelected(false);
                break;
            case 1:
                btn_main_group.setSelected(false);
                btn_mian_patient.setSelected(true);
                btn_mian_appointment.setSelected(false);
                btn_main_my.setSelected(false);
                break;
            case 2:
                btn_main_group.setSelected(false);
                btn_mian_patient.setSelected(false);
                btn_mian_appointment.setSelected(true);
                btn_main_my.setSelected(false);
                break;
            case 3:
                btn_main_group.setSelected(false);
                btn_mian_patient.setSelected(false);
                btn_mian_appointment.setSelected(false);
                btn_main_my.setSelected(true);
                break;
        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_main_group:
                setFragment(MAIN_BUTTON);
                break;
            case R.id.btn_mian_patient:
                setFragment(PATIRENT_BUTTON);
                break;
            case R.id.btn_mian_appointment:
                setFragment(APPOINTMENT_BUTTON);
                break;
            case R.id.btn_main_my:
                setFragment(MY_BUTTON);
                break;
        }
    }




    //切换fragment 的方法
    public void setFragment(int statue) {

        fragmenttransaction = fragmentmanager.beginTransaction();
        switch (statue) {
            case 0:

                if (homeFfragment == null) {
                    homeFfragment = new HomeFfragment();
                }
                fragmenttransaction.replace(R.id.fl_mian_frag, homeFfragment, "first");
                setStatue(MAIN_BUTTON);
                setTitle(getString(R.string.mian_button));

                break;
            case 1:
                if (patient_fragment == null) {
                    patient_fragment = new Patient_fragment();
                }
                fragmenttransaction.replace(R.id.fl_mian_frag, patient_fragment, "sencod");
                setStatue(PATIRENT_BUTTON);
                setTitle(getString(R.string.patient_button));

                break;
            case 2:

                if (appointment_fragment == null) {
                    appointment_fragment = new Appointment_fragment();
                }
                fragmenttransaction.replace(R.id.fl_mian_frag, appointment_fragment, "third");
                setStatue(APPOINTMENT_BUTTON);
                setTitle(getString(R.string.appointment_button));

                break;
            case 3:

                if (my_fragment == null) {
                    my_fragment = new My_fragment();
                }
                fragmenttransaction.replace(R.id.fl_mian_frag, my_fragment, "four");
                setStatue(MY_BUTTON);
                setTitle(getString(R.string.me_button));

                break;

            default:
                break;
        }
        fragmenttransaction.commit();
    }
}
